// pages/distribution/order.js
const numeral = require("../../../vendor/numeral/numeral.min")
const format = require('../../../utils/format.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.loadData()
    },

    loadData() {
        let {
            page = 1, nextPage = true, loading = false, list = [], page_num = 10
        } = this.data
        if (!nextPage && loading) return
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            loading: true
        })
        app.http.get("account/list", {
            data: {
                page: page,
                page_num: page_num,
                category_id: 1,
                type: 2,
            }
        }).then(res => {
            page++
            loading = false
            list.push(...res.data.list.map(o => {
                let t = new Date(o.order_info.created_at * 1000)
				o.order_info.created_at = format.dateformat.format(t, 'yyyy-MM-dd hh:mm:ss')
                o.order_info.item_list.map(i => {
                    i.price = numeral(i.price / 100).format("0,0.00")
                    return i
                })
                return o
            }))
            nextPage = res.data.next_page
            this.setData({
                page,
                loading,
                list,
                nextPage
            })
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            wx.hideLoading()
        })
    },

    onReachBottom() {
        this.loadData()
    },
    onPullDownRefresh() {
        this.setData({
            page: 1,
            loading: false,
            list: [],
            nextPage: true
        })
        this.loadData()
    }
})